<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-if="!isFollowed"
    @click="onFollow"
    :loading='is_follow_loading'
  >关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading='is_follow_loading'
  >已关注</van-button>
</template>

<script>
import { addFocus, removeFocus } from '@/api/user.js'
export default {
  props: {
    // 是否关注
    isFollowed: {
      type: Boolean,
      required: true
    },
    // 用户ID
    autId: {
      type: [Number, Object],
      required: true
    }
  },
  data () {
    return {
      // 用户关注加载状态
      is_follow_loading: false
    }
  },
  methods: {
    // 点击关注 取关 处理函数
    async onFollow () {
      this.is_follow_loading = true
      try {
        if (this.isFollowed) {
          await removeFocus(this.autId)
          // 弹窗提醒用户
          this.$toast('取消成功')
        } else {
          await addFocus(this.autId)
          // 弹窗提醒用户
          this.$toast('关注成功')
        }
        // 页面刷新
        this.$emit('updateIsFollowed', !this.isFollowed)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast('不能关注自己丫')
        }
        this.$toast('操作失败')
      }
      // 关闭loading状态
      this.is_follow_loading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  // 父组件如果用 v-mode 传递参数 用model 模块进行解析值和事件
  model: {
    // value的解析
    prop: 'isFollowed',
    // 事件的解析
    event: 'updateIsFollowed'
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
